import 'dart:convert';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_xupdate/flutter_xupdate.dart';

import 'package:package_info/package_info.dart';
import 'package:url_launcher/url_launcher.dart';

import 'bean/version_update_model.dart';
import 'utils/component/dialog/dialog_util.dart';
import 'utils/fluwx_util.dart';
import 'utils/httputils/httpmanager.dart';
import 'utils/httputils/resultdata.dart';
import 'utils/httputils/urlpath.dart';
import 'utils/jpusher.dart';
import 'utils/methodchannelmanager.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'
    show BMFMapSDK, BMF_COORD_TYPE;

initalizeView() {
  //设置导航栏
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      systemNavigationBarColor: Color(0xFF000000),
      systemNavigationBarDividerColor: null,
      statusBarColor: Colors.transparent,
      systemNavigationBarIconBrightness: Brightness.dark,
      statusBarIconBrightness: Brightness.dark,
      statusBarBrightness: Brightness.light,
    ),
  );
  // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
  //     overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top]);
  // MethodChannelManager.initChannel();
  // JPushMan().initJpush();
  // WXUtil().register();

  // Future.delayed(const Duration(seconds: 1)).then((value) {
  //   initXUpdate();
  // });
  // versionUpdate();

  if (Platform.isIOS) {
    // BMFMapSDK.setApiKeyAndCoordType(
    //     'jURqmIt3tGzd4TZlb9ztBswLcdGes7rE', BMF_COORD_TYPE.BD09LL);
  } else if (Platform.isAndroid) {
// Android 目前不支持接口设置Apikey,
// 请在主工程的Manifest文件里设置，详细配置方法请参考[https://lbs.baidu.com/ 官网][https://lbs.baidu.com/)demo
    BMFMapSDK.setCoordType(BMF_COORD_TYPE.BD09LL);
  }
}

///初始化
void initXUpdate() {
  if (Platform.isAndroid) {
    FlutterXUpdate.init(

            ///是否输出日志
            debug: kDebugMode,

            ///是否使用post请求
            isPost: false,

            ///post请求是否是上传json
            isPostJson: false,

            ///请求响应超时时间
            timeout: 25000,

            ///是否开启自动模式
            isWifiOnly: false,

            ///是否开启自动模式
            isAutoMode: false,

            ///需要设置的公共参数
            supportSilentInstall: false,

            ///在下载过程中，如果点击了取消的话，是否弹出切换下载方式的重试提示弹窗
            enableRetry: false)
        .then((value) {
      EasyLoading.showToast("初始化成功: $value");
    }).catchError((error) {
      print(error);
    });
  } else {
    // EasyLoading.showToast("ios暂不支持XUpdate更新");
  }
}

void versionUpdate() async {
  ResultData res = await HttpManager.getInstance()
      .get(UrlPath.checkUpdate, withLoading: false);
  var _data = jsonDecode(res.data);
  var md = VersionUpdateModel.fromJson(_data);
  if (md.code == 0) {
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      var _currVersion = packageInfo.version;
      var res = md.versionName!.compareTo(_currVersion);
      if (res == 1) {
        showAlert(
          title: '更新提示',
          msg: md.modifyContent,
          sureAction: () {
            if (md.appleUrl != null) {
              launch(md.appleUrl!);
            }
          },
        );
      }
    });
  }
}
